package com.inventory.spring.controller.product;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.inventory.form.ProductTypeForm;
import com.inventory.persistence.ProductType;
import com.inventory.service.product.ProductTypeService;

@Controller
public class ProductTypeController {
	@Autowired
	private ProductTypeService productTypeService;
	@Autowired
	private Validator productTypeValidator;
	
	private static final String PRODUCT_TYPE_URL ="product/productType";

	private static final int DEFAULT_PRODUCT_TYPE_ARRAY_LENGTH = 5;

	@RequestMapping(method = RequestMethod.GET, value = "/protected/editProductTypes.do")
	public String viewEditProductTypes(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<ProductType> productTypes = productTypeService.getAllProductTypes();
		ProductTypeForm productTypeForm = new ProductTypeForm();
		productTypeForm.setProductTypes(productTypes.toArray(new ProductType[productTypes.size()]));
		model.addAttribute("productTypeForm", productTypeForm);
		model.addAttribute("title", "Edit Product type");
		model.addAttribute("action", "updateProductType.do");
		return PRODUCT_TYPE_URL;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/protected/deleteProductTypes.do")
	public String viewDeleteProductTypes(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<ProductType> productTypes = productTypeService.getAllProductTypes();
		ProductTypeForm productTypeForm = new ProductTypeForm();
		productTypeForm.setProductTypes(productTypes.toArray(new ProductType[productTypes.size()]));
		model.addAttribute("productTypeForm", productTypeForm);
		model.addAttribute("title", "Delete Product type");
		model.addAttribute("action", "updateProductType.do");
		model.addAttribute("readOnly", "true");
		model.addAttribute("isDelete", "true");
		return PRODUCT_TYPE_URL;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/protected/viewProductTypes.do")
	public String viewProductTypes(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<ProductType> productTypes = productTypeService.getAllProductTypes();
		ProductTypeForm productTypeForm = new ProductTypeForm();
		productTypeForm.setProductTypes(productTypes.toArray(new ProductType[productTypes.size()]));
		model.addAttribute("productTypeForm", productTypeForm);
		model.addAttribute("title", "View Product type");
		model.addAttribute("action", "updateProductType.do");
		model.addAttribute("readOnly", "true");
		return PRODUCT_TYPE_URL;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/protected/addProductTypes.do")
	public String viewAddProductTypes(HttpServletRequest request, HttpServletResponse response, Model model) {
		ProductTypeForm productTypeForm = new ProductTypeForm();
		ProductType[] productTypes = new ProductType[DEFAULT_PRODUCT_TYPE_ARRAY_LENGTH];
		productTypeForm.setProductTypes(productTypes);
		model.addAttribute("productTypeForm", productTypeForm);
		model.addAttribute("title", "Add Product type");
		model.addAttribute("action", "insertProductType.do");
		return PRODUCT_TYPE_URL;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/protected/updateProductType.do")
	public String updateProductTypes(@ModelAttribute("productTypeForm") ProductTypeForm productTypeForm,
			HttpServletRequest request, HttpServletResponse response, Model model, BindingResult result) {
		productTypeValidator.validate(productTypeForm, result);
//		if (result.hasErrors()) {
//			return "companydetails";
//		}
		productTypeService.batchUpdateProductTypes(productTypeForm.getProductTypes());
		return "companydetails";
	}

	@RequestMapping(method = RequestMethod.GET, value = "/protected/deleteSubmitProductType.do")
	public String deleteProductTypes(@ModelAttribute("productTypeForm") ProductTypeForm productTypeForm,
			HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam("deleteId") String id) {
		productTypeService.deleteProductTypes(Long.valueOf(id));
		return viewDeleteProductTypes(request, response, model);
	}

	@RequestMapping(method = RequestMethod.POST, value = "/protected/insertProductType.do")
	public String insertProductTypes(@ModelAttribute("productTypeForm") ProductTypeForm productTypeForm,
			HttpServletRequest request, HttpServletResponse response, Model model) {
		productTypeService.batchInsertProductTypes(productTypeForm.getProductTypes());
		return "companydetails";
	}

	public void setProductTypeService(ProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}

	public void setProductTypeValidator(Validator productTypeValidator) {
		this.productTypeValidator = productTypeValidator;
	}

}
